package com.greate.community.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.greate.community.entity.Page;
import com.greate.community.entity.Result;
import com.greate.community.entity.User;
import com.greate.community.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户身份实名认证审核模块
 *
 * @author xzx
 * @date 2022/2/9 21:41
 */
@RestController
public class AuditController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 通过或驳回实名信息
     */
    @PostMapping("/admin/audit")
    public Result audit(@RequestBody HashMap<String, Integer> map) {
        int userId = map.get("userId");
        int auditStatus = map.get("auditStatus");

        if (auditStatus == 2) {
            userService.updateAuditStatus(userId, auditStatus);
            return new Result("success", Result.SUCCESS, "用户id--" + userId + "--审核通过成功");
        } else if (auditStatus == 3) {
            userService.updateAuditStatus(userId, auditStatus);
            return new Result("success", Result.SUCCESS, "用户id--" + userId + "--审核驳回成功");
        } else {
            return new Result("fail", "1", "the request parameters are invalid");
        }
    }

    /**
     * 查看用户实名列表简略信息
     */
    @GetMapping("/admin/user/brief")
    public Result getBriefUsers(Page page, int auditStatus) {
        if (auditStatus == 0 || auditStatus == 1 || auditStatus == 2 || auditStatus == 3) {
            page.setPath("/admin/user/brief");
            page.setRows(userService.findRealNameRows(auditStatus));

            List<User> briefUserList = userService.findRealNameList(auditStatus, page.getOffset(), page.getLimit(), 0);
            List<Map<String, Object>> resultBriefUserList = new ArrayList<>();

            for (User user : briefUserList) {
                HashMap<String, Object> resultBriefUser = new HashMap<>();
                resultBriefUser.put("id", user.getId());
                resultBriefUser.put("username", user.getUsername());
                resultBriefUser.put("email", user.getEmail());
                resultBriefUserList.add(resultBriefUser);
            }

            JSONArray result = (JSONArray) JSONArray.toJSON(resultBriefUserList);
            page.setResult(result);

            return new Result("success", Result.SUCCESS, page);
        } else {
            return new Result("fail", "1", "the request parameters are invalid");
        }
    }

    /**
     * 查看用户实名列表详细信息
     */
    @GetMapping("/admin/user/detailed")
    public Result getDetailedUsers(Page page, int auditStatus) {
        if (auditStatus == 0 || auditStatus == 1 || auditStatus == 2 || auditStatus == 3) {
            page.setPath("/admin/user/detailed");
            page.setRows(userService.findRealNameRows(auditStatus));

            List<User> detailedUserList = userService.findRealNameList(auditStatus, page.getOffset(), page.getLimit(), 1);
            List<Map<String, Object>> resultDetailedUserList = new ArrayList<>();

            for (User user : detailedUserList) {
                HashMap<String, Object> resultDetailedUser = new HashMap<>();
                resultDetailedUser.put("id", user.getId());
                resultDetailedUser.put("username", user.getUsername());
                resultDetailedUser.put("email", user.getEmail());
                resultDetailedUser.put("studentCardUrl", user.getStudentCardUrl());
                resultDetailedUser.put("studentCode", user.getStudentCode());
                resultDetailedUser.put("realName", user.getRealName());
                resultDetailedUser.put("institute", user.getInstitute());
                resultDetailedUser.put("professionalClass", user.getProfessionalClass());
                resultDetailedUser.put("auditStatus", user.getAuditStatus());
                resultDetailedUserList.add(resultDetailedUser);
            }

            JSONArray result = (JSONArray) JSONArray.toJSON(resultDetailedUserList);
            page.setResult(result);

            return new Result("success", Result.SUCCESS, page);
        } else {
            return new Result("fail", "1", "the request parameters are invalid");
        }
    }

    /**
     * 依据userId查询用户实名详细信息
     */
    @GetMapping("/admin/user/detailedById")
    public Result getDetailedUserById(int userId) {
        User user = userService.findRealNameUserById(userId);
        if (user == null) {
            return new Result("fail", "1", "用户不存在");
        }
        HashMap<String, Object> resultDetailedUser = new HashMap<>();
        resultDetailedUser.put("id", user.getId());
        resultDetailedUser.put("username", user.getUsername());
        resultDetailedUser.put("email", user.getEmail());
        resultDetailedUser.put("studentCardUrl", user.getStudentCardUrl());
        resultDetailedUser.put("studentCode", user.getStudentCode());
        resultDetailedUser.put("realName", user.getRealName());
        resultDetailedUser.put("institute", user.getInstitute());
        resultDetailedUser.put("professionalClass", user.getProfessionalClass());
        resultDetailedUser.put("auditStatus", user.getAuditStatus());

        return new Result("success", Result.SUCCESS, resultDetailedUser);
    }
}
